<template>
  <header class="app-header">
    <div class="header-container">
      <div class="header-left">
        <router-link to="/" class="logo">
          <div class="logo-text">
            <span class="logo-main">WWH</span>
            <span class="logo-divider">·</span>
            <span class="logo-sub">OJ</span>
          </div>
        </router-link>
      </div>

      <nav class="nav-menu">
        <router-link to="/" class="nav-item" active-class="active">
          <el-icon><House /></el-icon>
          <span>首页</span>
        </router-link>
        <router-link to="/problems" class="nav-item" active-class="active">
          <el-icon><Collection /></el-icon>
          <span>题库</span>
        </router-link>
        <router-link to="/favorites" class="nav-item" active-class="active">
          <el-icon><Star /></el-icon>
          <span>我的收藏</span>
        </router-link>
        <router-link to="/contests" class="nav-item" active-class="active">
          <el-icon><Trophy /></el-icon>
          <span>竞赛</span>
        </router-link>
        <router-link to="/rank" class="nav-item" active-class="active">
          <el-icon><Medal /></el-icon>
          <span>排名</span>
        </router-link>
      </nav>

      <div class="header-right">
        <div v-if="isLoggedIn" class="user-section">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="36" :src="userInfo.avatar" class="user-avatar">
                {{ userInfo.username.charAt(0).toUpperCase() }}
              </el-avatar>
              <span class="username">{{ userInfo.nick_name || userInfo.username }}</span>
              <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  <span>个人中心</span>
                </el-dropdown-item>
                <el-dropdown-item command="submissions">
                  <el-icon><Document /></el-icon>
                  <span>提交记录</span>
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  <span>设置</span>
                </el-dropdown-item>
                <el-dropdown-item 
                  v-if="isAdmin" 
                  command="admin"
                >
                  <el-icon><Setting /></el-icon>
                  <span>管理后台</span>
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div v-else class="auth-buttons">
          <el-button text @click="$router.push('/login')">登录</el-button>
          <el-button type="primary" class="register-btn" @click="$router.push('/register')">注册</el-button>
        </div>

        <el-button class="mobile-menu-btn" text @click="toggleMobileMenu">
          <el-icon><Menu /></el-icon>
        </el-button>
      </div>
    </div>

    <!-- 移动端菜单 -->
    <transition name="slide-fade">
      <div v-if="showMobileMenu" class="mobile-menu">
        <nav class="mobile-nav">
          <router-link to="/" class="mobile-nav-item" @click="closeMobileMenu">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </router-link>
          <router-link to="/problems" class="mobile-nav-item" @click="closeMobileMenu">
            <el-icon><Collection /></el-icon>
            <span>题库</span>
          </router-link>
          <router-link to="/favorites" class="mobile-nav-item" @click="closeMobileMenu">
            <el-icon><Star /></el-icon>
            <span>我的收藏</span>
          </router-link>
          <router-link to="/contests" class="mobile-nav-item" @click="closeMobileMenu">
            <el-icon><Trophy /></el-icon>
            <span>竞赛</span>
          </router-link>
          <router-link to="/rank" class="mobile-nav-item" @click="closeMobileMenu">
            <el-icon><Medal /></el-icon>
            <span>排名</span>
          </router-link>
          <template v-if="!isLoggedIn">
            <router-link to="/login" class="mobile-nav-item" @click="closeMobileMenu">
              <el-icon><User /></el-icon>
              <span>登录</span>
            </router-link>
            <router-link to="/register" class="mobile-nav-item highlight" @click="closeMobileMenu">
              <el-icon><Plus /></el-icon>
              <span>注册</span>
            </router-link>
          </template>
        </nav>
      </div>
    </transition>
  </header>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { storeToRefs } from 'pinia'
import authApi from '../api/auth'
import { 
  House, 
  Collection, 
  Star, 
  Trophy, 
  Medal, 
  Setting, 
  User, 
  Document, 
  SwitchButton, 
  Menu, 
  ArrowDown 
} from '@element-plus/icons-vue'

const router = useRouter()
const showMobileMenu = ref(false)
const userStore = useUserStore()
const { userInfo, isLoggedIn } = storeToRefs(userStore)
const isAdmin = computed(() => userInfo.value.is_staff === true)

// 获取用户信息
const fetchUserInfo = async () => {
  await userStore.fetchUserInfo()
}

const toggleMobileMenu = () => {
  showMobileMenu.value = !showMobileMenu.value
}

const closeMobileMenu = () => {
  showMobileMenu.value = false
}

const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'submissions':
      router.push('/submissions')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'admin':
      router.push('/admin/problems')
      break
    case 'logout':
      try {
        await authApi.logout()
        userStore.logout()
        router.push('/login')
      } catch (error) {
        console.error('Logout failed:', error)
      }
      break
  }
}

// 初始化时获取用户信息
onMounted(() => {
  fetchUserInfo()
})
</script>

<style scoped>
.app-header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  text-decoration: none;
  color: #333;
  font-size: 24px;
  font-weight: bold;
}

.logo-text {
  display: flex;
  align-items: center;
  gap: 4px;
}

.logo-main {
  color: #409eff;
}

.logo-divider {
  color: #909399;
}

.logo-sub {
  color: #606266;
}

.nav-menu {
  display: flex;
  gap: 24px;
  margin-left: 40px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 4px;
  text-decoration: none;
  color: #606266;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-item:hover {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
}

.nav-item.active {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-section {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #606266;
}

.dropdown-icon {
  color: #909399;
  font-size: 12px;
  margin-left: 4px;
}

.auth-buttons {
  display: flex;
  gap: 12px;
  align-items: center;
}

.register-btn {
  border-radius: 4px;
}

.mobile-menu-btn {
  display: none;
}

@media (max-width: 768px) {
  .nav-menu {
    display: none;
  }

  .mobile-menu-btn {
    display: block;
  }

  .mobile-menu {
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    padding: 16px;
  }

  .mobile-nav {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .mobile-nav-item {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: #606266;
    font-size: 14px;
    padding: 12px;
    border-radius: 4px;
    transition: all 0.3s;
  }

  .mobile-nav-item:hover {
    color: #409eff;
    background-color: rgba(64, 158, 255, 0.1);
  }

  .mobile-nav-item.active {
    color: #409eff;
    background-color: rgba(64, 158, 255, 0.1);
  }
}

.slide-fade-enter-active,
.slide-fade-leave-active {
  transition: all 0.3s ease;
}

.slide-fade-enter-from,
.slide-fade-leave-to {
  transform: translateY(-20px);
  opacity: 0;
}
</style>
